<template>
    <div class="account_type_form_container">
        <el-form ref="accountTypeForm" :rules="rules" :model="formData" label-width="100px">
            <el-form-item label="对账类型" prop="key2">
                <el-select v-model="formData.key2" placeholder="请选择对账" style="width: 100%;">
                    <el-option label="收款" value="cash_in" />
                    <el-option label="付款" value="cash_out" />
                </el-select>
            </el-form-item>
            <el-form-item label="类型编码" prop="key3">
                <el-input v-model="formData.key3" autocomplete="off" placeholder="请输入类型编码" />
            </el-form-item>
            <el-form-item label="类型名称" prop="display_text">
                <el-input v-model="formData.display_text" autocomplete="off" placeholder="请输入类型名称" />
            </el-form-item>
            <el-form-item label="算术运算符" prop="value">
                <el-select v-model="formData.value" placeholder="请选择算术运算符" style="width: 100%;">
                    <el-option label="+" value="+" />
                    <el-option label="-" value="-" />
                </el-select>
            </el-form-item>
            <el-form-item label="排序" prop="sn">
                <el-input v-model="formData.sn" autocomplete="off" placeholder="请输入排序" v-int />
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "AccountTypeForm",
    props: {
        row: {
            type: Object,
            default: null,
        },
    },
    data() {
        return {
            rules: {
                key2: [
                    { required: true, message: "请选择类型", trigger: "change" },
                ],
                key3: [
                    { required: true, message: "请输入类型编码", trigger: "blur" },
                    {
                        pattern: /^[A-Za-z0-9+-_.]{1,32}$/,
                        message:
                            "最大32位字符，大小写英文、数字或者以下特殊字符（+-_.），不能包含特殊字符",
                    },
                ],
                display_text: [
                    { required: true, message: "请输入类型名称", trigger: "blur" },
                    //目前只兼容中文、大小写英文、数字
                    {
                        // pattern: /^[A-Za-z0-9\u4e00-\u9fa5]{1,50}$/, message:'最大 50 位大小写英文、数字或者其它地区语言字符，不能包含特殊字符'
                        max: 50,
                        message: "最大 50 位字符，内容过长，请删减",
                        trigger: "blur",
                    },
                ],
                value: [
                    { required: true, message: "请选择算术运算符", trigger: "change" },
                ],
            },
            formData: {},
        };
    },
    watch: {
        row: {
            handler() {
                this.init();
            },
            deep: true,
            immediate: true,
        },
    },
    methods: {
        init() {
            const _this = this;
            if (!_this.row) {
                return;
            }
            if (_this.$refs.accountTypeForm) {
                _this.$refs.accountTypeForm.clearValidate();
            }
            _this.formData = $.extend({}, _this.row);
            if (!_this.$utils.isNotEmpty(_this.formData.key2)) {
                _this.formData = $.extend({}, _this.formData, {
                    key2: 'cash_in',
                });
            }
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('accountTypeForm');
            if (valid) {
                let subData = $.extend({}, _this.formData);
                return _this.fetchData(_this.$api.API_SaveJxcFinanceAccountType, subData).then(() => {
                    _this.$message.success('保存成功！');
                    return true;
                }).catch(() => {
                    return false;
                });
            }
            return valid;
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.account_type_form_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
